package demo;

public class Text {
    public static void main(String[] args) {

        int[] arr = {1,2,3,4,5,6,7,8,9};
        int n = 7;
        int result = binSearch(arr, n);
        System.out.println(result);
    }

    private static int binSearch(int[] arr, int n) {
        int min = 0;
        int max = arr.length - 1;

        while (true) {
            if (min > max) {
                return -1;
            }
            int mid = (min + max) / 2;
            if (arr[mid] < n) {
                //在右侧
                min = mid + 1;
            } else if (arr[mid] > n) {
                //在左侧
                max = mid - 1;
            } else {
                return mid;
            }
        }
    }
}
